﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Hosting;

namespace KaytechSoftware.Cms.Common.Helpers
{
    public class PathHelper
    {
        public static HttpServerUtility Server { get; set; }

        public static string PhotoPathForServer(long photoAlbumId, long photoId)
        {
            return Server.MapPath(string.Format(@"Images\{0}\{1}.jpg", photoAlbumId, photoId));
        }

        public static string PhotoPathForWeb(long photoAlbumId, long photoId, long version)
        {
            return string.Format(@"{0}/Images/{1}/{2}.jpg?version={3}", HostingEnvironment.ApplicationVirtualPath, photoAlbumId, photoId, version);
        }

        public static string PhotoThumbPathForServer(long photoAlbumId, long photoId)
        {
            return Server.MapPath(string.Format(@"Images\{0}\Thumbs\{1}.jpg", photoAlbumId, photoId));
        }

        public static string PhotoThumbPathForWeb(long photoAlbumId, long photoId, long version)
        {
            return string.Format(@"{0}/Images/{1}/Thumbs/{2}.jpg?version={3}", HostingEnvironment.ApplicationVirtualPath, photoAlbumId, photoId, version);
        }

        public static string PhotoSmallPathForServer(long photoAlbumId, long photoId)
        {
            return Server.MapPath(string.Format(@"Images\{0}\Small\{1}.jpg", photoAlbumId, photoId));
        }

        public static string PhotoSmallPathForWeb(long photoAlbumId, long photoId, long version)
        {
            return string.Format(@"{0}/Images/{1}/Small/{2}.jpg?version={3}", HostingEnvironment.ApplicationVirtualPath, photoAlbumId, photoId, version);
        }

        public static void AssurePhysicalPathExists(string path)
        {
            if (path.Length <= 3) 
                return;

            var pos = 3;
            while (pos < path.Length - 1 && (pos = path.IndexOf('\\', pos + 1)) > 0)
            {
                var tmpPath = path.Substring(0, pos);
                if (!Directory.Exists(tmpPath))
                {
                    Directory.CreateDirectory(tmpPath);
                }
            }
        }
    }
}
